﻿<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button v-if="hasPerm('Base_VIPUserInfo.Add')" type="primary" icon="plus" @click="handleAdd()">新建</a-button>
      <a-button
        v-if="hasPerm('Base_VIPUserInfo.Delete')"
        type="primary"
        icon="minus"
        @click="handleDelete(selectedRowKeys)"
        :disabled="hasSelected()"
        :loading="loading"
      >删除
      </a-button>
      <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
    </div>

    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <a-form-item label="查询类别">
              <a-select allowClear v-model="queryParam.condition">
                <!--<a-select-option key="Code">自定义编号</a-select-option>-->
                <!--<a-select-option key="WxCode">微信code</a-select-option>-->
                <a-select-option key="Name">姓名</a-select-option>
                <a-select-option key="Sex">性别</a-select-option>
                <a-select-option key="Phone">电话</a-select-option>
                <!--<a-select-option key="Address">地址</a-select-option>-->
                <a-select-option key="Email">邮箱</a-select-option>
                <a-select-option key="Company">所属公司</a-select-option>
                <!--<a-select-option key="Icon">头像地址</a-select-option>-->
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input v-model="queryParam.keyword" placeholder="关键字" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="() => {this.pagination.current = 1; this.getDataList()}">查询</a-button>
            <a-button style="margin-left: 8px" @click="() => {queryParam = {};this.getDataList()}">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      :scroll="{y:530}"
      :columns="columns"
      :rowKey="row => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :bordered="true"
      size="small"
    >
      <span slot="Birth" slot-scope="Birth">{{ timeFormat(Birth) }}</span>
      <span slot="showUserIcon" slot-scope="text">
        <a-popover>
          <img class="userIconSmall" :src="text" />
          <template #content class="userIconLarge">
            <img :src="text" />
          </template>
        </a-popover>
      </span>
      <span slot="UpdateTime" slot-scope="UpdateTime">{{ timeFormat(UpdateTime) }}</span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-if="hasPerm('Base_VIPUserInfo.Edit')" @click="handleEdit(record.Id)">编辑</a>
          <a-divider v-if="hasPerm('Base_VIPUserInfo.Edit')&&hasPerm('Base_VIPUserInfo.Delete')" type="vertical" />
          <a v-if="hasPerm('Base_VIPUserInfo.Delete')" @click="handleDelete([record.Id])">删除</a>
        </template>
      </span>

    </a-table>

    <edit-form ref="editForm" :parentObj="this"></edit-form>
  </a-card>
</template>

<script>
import EditForm from './EditForm'
import moment from 'moment'

const columns = [
  // { title: '自定义编号', dataIndex: 'Code', width: '10%' },
  // { title: '微信code', dataIndex: 'WxCode', width: '10%' },
  { title: '头像', dataIndex: 'Icon', width: '3%', scopedSlots: { customRender: 'showUserIcon' } },
  { title: '昵称', dataIndex: 'NickName', width: '10%' },
  { title: '姓名', dataIndex: 'Name', width: '8%' },
  { title: '性别', dataIndex: 'Sex', width: '5%' },
  { title: '出生日期', dataIndex: 'Birth', width: '10%', scopedSlots: { customRender: 'Birth' } },
  { title: '电话', dataIndex: 'Phone', width: '10%' },
  // { title: '邮箱', dataIndex: 'Email', width: '10%' },
  { title: '所属公司', dataIndex: 'Company', width: '10%' },
  { title: '地址', dataIndex: 'Address' },
  {
    title: '最后登录时间',
    dataIndex: 'UpdateTime',
    width: '10%',
    align: 'center',
    scopedSlots: { customRender: 'UpdateTime' }
  },
  { title: '操作', dataIndex: 'action', width: '6%', scopedSlots: { customRender: 'action' } }
]

export default {
  components: {
    EditForm
  },
  mounted () {
    this.getDataList()
  },
  data () {
    return {
      data: [],
      pagination: {
        current: 1,
        pageSize: 20,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['10', '20', '30', '40', '50', '100'], // 设置pageSize的可选值，页面啊可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'Id', order: 'desc' },
      loading: false,
      columns,
      queryParam: {},
      selectedRowKeys: []
    }
  },
  methods: {
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post('/Base_VIPUser/Base_VIPUserInfo/GetDataList', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order || 'desc',
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected () {
      return this.selectedRowKeys.length <= 0
    },
    handleAdd () {
      this.$refs.editForm.openForm()
    },
    handleEdit (id) {
      this.$refs.editForm.openForm(id)
    },
    handleDelete (ids) {
      const thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk () {
          return new Promise((resolve, reject) => {
            thisObj.$http.post('/Base_VIPUser/Base_VIPUserInfo/DeleteData', ids).then(resJson => {
              resolve()

              if (resJson.Success) {
                thisObj.$message.success('操作成功!')

                thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
          })
        }
      })
    },
    timeFormat (val) { // 时间格式化函数
      if (val) {
        return moment(val).format('YYYY-MM-DD')
      }
      return null
    }
  }
}
</script>
<style scoped>
.userIconSmall {
  width: 30px;
  height: 30px;
}

.userIconLarge {
  width: 100px;
  height: 100px;
}
</style>
